package com.app.couponsharelife.ui.main.home.message

import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import com.app.couponsharelife.R
import com.app.couponsharelife.base.LazyFragment
import com.scwang.smartrefresh.layout.api.RefreshFooter
import com.scwang.smartrefresh.layout.api.RefreshHeader
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.constant.RefreshState
import com.scwang.smartrefresh.layout.listener.OnMultiPurposeListener
import kotlinx.android.synthetic.main.base_refresh_head.*
import kotlinx.android.synthetic.main.fragment_system_msg.*

/**
 *Created by xct on 2019/1/26
 *Description:系统消息列表页面
 */
class SystemMSGFragment:LazyFragment() {
    private var animationDrawable:AnimationDrawable?=null
    private var pageIndex=1
    companion object {
        fun newInstance(): SystemMSGFragment {
            val fragment = SystemMSGFragment()
            fragment.arguments = Bundle()
            return fragment
        }
    }
    override val viewLayoutId: Int
        get() = R.layout.fragment_system_msg

    override fun initViews() {
        setRefresh()
    }

    override fun lazyLoad() {

    }
    private fun setRefresh(){
        hotsale_refreshimg.setImageResource(R.drawable.anim_refresh)
        animationDrawable=hotsale_refreshimg.drawable as AnimationDrawable
        system_msg_smart.setOnMultiPurposeListener(object : OnMultiPurposeListener {
            override fun onFooterMoving(footer: RefreshFooter?, isDragging: Boolean, percent: Float, offset: Int, footerHeight: Int, maxDragHeight: Int) {

            }

            override fun onHeaderStartAnimator(header: RefreshHeader?, headerHeight: Int, maxDragHeight: Int) {
                //animationDrawable!!.start()
            }

            override fun onFooterReleased(footer: RefreshFooter?, footerHeight: Int, maxDragHeight: Int) {

            }

            override fun onStateChanged(refreshLayout: RefreshLayout, oldState: RefreshState, newState: RefreshState) {

            }

            override fun onHeaderMoving(header: RefreshHeader?, isDragging: Boolean, percent: Float, offset: Int, headerHeight: Int, maxDragHeight: Int) {
                setAnima(percent)
            }

            override fun onFooterFinish(footer: RefreshFooter?, success: Boolean) {

            }

            override fun onFooterStartAnimator(footer: RefreshFooter?, footerHeight: Int, maxDragHeight: Int) {

            }

            override fun onHeaderReleased(header: RefreshHeader?, headerHeight: Int, maxDragHeight: Int) {
                animationDrawable!!.start()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                //getGoodsList(false)
                system_msg_smart.finishLoadMore(2000)
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageIndex=1
                //getGoodsList(true)
                system_msg_smart.finishRefresh(2000)
            }

            override fun onHeaderFinish(header: RefreshHeader?, success: Boolean) {
                hotsale_refreshimg.setImageResource(R.drawable.refresh_0)
                animationDrawable!!.stop()
            }
        })
    }
    private fun setAnima(vlaue:Float){
        val layoutParams=hotsale_refreshimg.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.matchConstraintPercentHeight=vlaue/2
        layoutParams.dimensionRatio="0.875:1"
        hotsale_refreshimg.layoutParams=layoutParams
    }
}